package top.xzxsrq.datahelp.zhixin.module.socket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;
import top.xzxsrq.datahelp.zhixin.ZhixinApplication;
import top.xzxsrq.datahelp.zhixin.module.socket.entity.MessageTypeEntity;
import top.xzxsrq.datahelp.zhixin.module.socket.handler.DataTypeHandler;
import top.xzxsrq.datahelp.zhixin.module.socket.handler.SocketMessageBaseHandler;
import top.xzxsrq.datahelp.zhixin.module.socket.service.WebSocketService;
import top.xzxsrq.datahelp.zhixin.myenum.MessageTypeEnum;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 消息接受处理类
 *
 * @program: datahelp
 * @create: 2021-11-09 20:35
 **/
@Component
@Slf4j
public class WebSocketHandler extends AbstractWebSocketHandler {

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private ZhixinApplication zhixinApplication;

    @Autowired
    private WebSocketService webSocketService;

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        //socket连接成功后触发
        log.info("建立websocket连接");
        WebSocketSessionManager.add(session.getId(), session);
        MessageTypeEntity messageTypeEntity = new MessageTypeEntity();
        messageTypeEntity.setMessageType(MessageTypeEnum.DATA_TYPE.getType());
        messageTypeEntity.setValue(zhixinApplication.getDataType());
        webSocketService.sendMsg(session,JSON.toJSONString(messageTypeEntity));
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        // 客户端发送普通文件信息时触发
        log.info("发送文本消息");
        // 获得客户端传来的消息
        String payload = message.getPayload();
        log.info("服务端接收到消息 " + payload);
        MessageTypeEntity messageTypeEntity = JSON.parseObject(payload, MessageTypeEntity.class);
        Map<String, SocketMessageBaseHandler> beansOfType = applicationContext.getBeansOfType(SocketMessageBaseHandler.class);
        for (SocketMessageBaseHandler value : beansOfType.values()) {
            value.sockerGetMessage(messageTypeEntity, session);
        }
    }

    @Override
    protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
        //客户端发送二进信息是触发
        log.info("发送二进制消息");
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        //异常时触发
        log.error("异常处理");
        WebSocketSessionManager.removeAndClose(session.getId());
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        // socket连接关闭后触发
        log.info("关闭websocket连接");
        WebSocketSessionManager.removeAndClose(session.getId());
    }
}
